using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace DesktopWidgetApp.Client.ViewModels
{
    /// <summary>
    /// 任务状态枚举
    /// </summary>
    public enum TaskStatus
    {
        All,
        Todo,
        InProgress,
        Completed,
        Paused,
        Cancelled
    }

    /// <summary>
    /// 任务优先级枚举
    /// </summary>
    public enum Priority
    {
        All,
        Low,
        Medium,
        High,
        Urgent
    }

    /// <summary>
    /// 任务项视图模型
    /// </summary>
    public class TaskItemViewModel : INotifyPropertyChanged
    {
        private string? _id;
        private string? _title;
        private string? _description;
        private TaskStatus _status;
        private Priority _priority;
        private DateTime? _dueDate;
        private string[]? _tags;
        private bool _isSelected;
        private bool _isCompleted;
        private bool _isDueDatePassed;

        public string Id
        {
            get => _id;
            set => SetProperty(ref _id, value);
        }

        public string Title
        {
            get => _title;
            set => SetProperty(ref _title, value);
        }

        public string Description
        {
            get => _description;
            set => SetProperty(ref _description, value);
        }

        public TaskStatus Status
        {
            get => _status;
            set 
            {
                SetProperty(ref _status, value);
                IsCompleted = _status == TaskStatus.Completed;
            }
        }

        public Priority Priority
        {
            get => _priority;
            set => SetProperty(ref _priority, value);
        }

        public DateTime? DueDate
        {
            get => _dueDate;
            set 
            {
                SetProperty(ref _dueDate, value);
                UpdateDueDateStatus();
            }
        }

        public string[]? Tags
        {
            get => _tags;
            set => SetProperty(ref _tags, value);
        }

        public bool IsSelected
        {
            get => _isSelected;
            set => SetProperty(ref _isSelected, value);
        }

        public bool IsCompleted
        {
            get => _isCompleted;
            private set => SetProperty(ref _isCompleted, value);
        }

        public bool IsDueDatePassed
        {
            get => _isDueDatePassed;
            private set => SetProperty(ref _isDueDatePassed, value);
        }

        // 用于UI显示的优先级文本
        public string PriorityDisplayName => GetPriorityDisplayName();

        // 用于UI显示的状态文本
        public string StatusDisplayName => GetStatusDisplayName();

        // 用于UI显示的截止日期文本
        public string DueDateDisplay => DueDate.HasValue ? DueDate.Value.ToString("yyyy-MM-dd") : "无";

        private string GetPriorityDisplayName()
        {
            switch (Priority)
            {
                case Priority.Low:
                    return "低";
                case Priority.Medium:
                    return "中";
                case Priority.High:
                    return "高";
                case Priority.Urgent:
                    return "紧急";
                default:
                    return "未设置";
            }
        }

        private string GetStatusDisplayName()
        {
            switch (Status)
            {
                case TaskStatus.Todo:
                    return "待办";
                case TaskStatus.InProgress:
                    return "进行中";
                case TaskStatus.Completed:
                    return "已完成";
                case TaskStatus.Paused:
                    return "已暂停";
                case TaskStatus.Cancelled:
                    return "已取消";
                default:
                    return "未知";
            }
        }

        private void UpdateDueDateStatus()
        {
            IsDueDatePassed = DueDate.HasValue && DueDate.Value < DateTime.Now && Status != TaskStatus.Completed;
        }

        public event PropertyChangedEventHandler? PropertyChanged;

        protected void SetProperty<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
        {
            if (!Equals(field, value))
            {
                field = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}